package hzmg.akkacloud.server.core.service;

import com.hzmg.akkacloud.common.exception.AkkaCloudException;
import hzmg.akkacloud.server.persistence.remote.model.ConfigInfoDO;
import hzmg.akkacloud.server.persistence.remote.repository.ConfigInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 用户服务
 *
 * @author ozh
 * @since 2022/11/15
 */
@Service
@Slf4j
public class ConfigService {

    @Resource
    private ConfigInfoRepository configInfoRepository;

    public void create(ConfigInfoDO configInfoDO) {
        configInfoDO.setGmtCreate(new Date());
        configInfoDO.setGmtModified(configInfoDO.getGmtCreate());
        try {
            configInfoRepository.saveAndFlush(configInfoDO);
        }catch (Exception e){
            throw new AkkaCloudException(e.toString());
        }

        //拼装文件名
        String configInfoName = configInfoDO.getDataId() + "." + configInfoDO.getType();
        try{
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:/" + configInfoName));
            bufferedOutputStream.write(configInfoDO.getContent().getBytes());
            bufferedOutputStream.close();
        }catch (IOException e){
            throw new AkkaCloudException(e.toString());
        }
    }

}
